package com.situ.allaia.interceptor;

import com.situ.allaia.model.AdminUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor {
    public static final String CURRENT_ADMIN_LOGIN_USER_KEY = "@#current_admin_user_login";
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        AdminUser user = (AdminUser) session.getAttribute(CURRENT_ADMIN_LOGIN_USER_KEY);
        if (user==null){
            response.sendRedirect(request.getContextPath()+"/admin_user/login");
            return false;
        }
        return true;
    }
}
